package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlw.hr_man.Model.EmployeeModel;
import com.hlw.hr_man.Model.SalaryModel;
import com.hlw.hr_man.service.SalaryService;
import com.hlw.hr_man.serviceImpl.SalaryServiceImpl;

import net.sf.json.JSONObject;

public class FindSalarycontroller extends HttpServlet{
	SalaryService salservice = new SalaryServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		int start = (page -1)*pageSize;
		JSONObject salarys = null;
		String  date = req.getParameter("date");
		String value = req.getParameter("value");
		String deptname = req.getParameter("deptname");
		if( value == null && date != null && deptname ==null){
			salarys = salservice.getSalaryByDate(date, start, pageSize);
			pw.write(salarys.toString());
		}
		if(value != null && date != null && deptname ==null){
			salarys = salservice.getSalaryByValue(date, value, start, pageSize);
			pw.write(salarys.toString());
		}
		if(value == null && date == null && deptname ==null){
			EmployeeModel emp = new EmployeeModel();
			emp = (EmployeeModel)req.getSession().getAttribute("loginEmployee");
			int e_id = emp.getE_id();
			int role_id = emp.getRole_id();
			String all = req.getParameter("all");
			if( all != null && all.equals("1")){
				salarys = salservice.findAllSalary(start, pageSize);
				pw.write(salarys.toString());
			}else{
				salarys = salservice.findCurrentSalary(e_id,start, pageSize);
				pw.write(salarys.toString());
			}
		}
		if(value == null && date == null && deptname != null){
			salarys = salservice.getSalaryByDept(deptname, start, pageSize);
			pw.write(salarys.toString());
		}
	}
}
